import axios from "axios";

const service = axios.create({
  baseURL:
    "https://www.fastmock.site/mock/6113cd6be8af08ab176c9b6d0b808fa6/api",
  timeout: 3000,
  headers: {
    "Content-Type": "application/json;charset=utf-8",
  },
});

//请求拦截
service.interceptors.request.use((config) => {
  config.headers = config.headers || {};
  if (sessionStorage.getItem("token")) {
    config.headers.token = sessionStorage.getItem("token");
  }
  return config;
});

//响应拦截
service.interceptors.response.use(
  (res) => {
    const code: number = res.data.code;
    if (code != 200) {
      return Promise.reject(res.data);
    }
    return res.data;
  },
  (err) => {
    console.log(err);
  }
);

export default service;
